import * as dictionary from './dictionary'
import * as utils from './utils'
// 隐藏手机号的部分数字
exports.hidePartNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{3})\d{4}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '****' + $2
  })
}
// 通知大于99时，显示99
exports.noticeMaxLimit = function (val) {
  if (val - 99 > 0) {
    return 99
  } else {
    return val
  }
}
// 隐藏部分姓名
exports.hidePartName = function (val) {
  if (!val) {
    return ''
  }
  let newValue = val[0]
  if (val.length === 1) {
    newValue = '*'
  }
  for (let i = 1; i < val.length; i++) {
    newValue += '*'
  }
  return newValue
}

// 隐藏身份证的部分数字
exports.hidePartIdCardNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{6})\d{8}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '********' + $2
  })
}

// 去除时间的秒
exports.hideSeconds = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3) {
    return $1 + ':' + $2
  })
  return val
}

// 保存两位小数
exports.byteToKB = function (val) {
  return (val / 1024).toFixed(2)
}

// 去除时间
exports.hideTime = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*)\s(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3, $4) {
    return $1
  })
  return val
}

// 时间戳转为时间 yyyy-MM-dd hh:mm:ss
exports.timestampToDate = utils.timestampToDate
/**
 * 计算当前时间较发布时间过去了多久，如一周前、一小时前等
 *
 */
exports.getDateDiff = utils.getDateDiff

// 转义 &lt;
exports.HTMLDecode = function (text) {
  var temp = document.createElement('div')
  temp.innerHTML = text
  var output = temp.innerText || temp.textContent
  temp = null
  return output
}

// 价格折扣 percent: 百分比 如80 代表80%
exports.discount = function (value, percent) {
  if (percent && percent != '') {
    return (value * (percent / 100)).toFixed(2)
  }
  return value
}

exports.removeBr = function (value) {
  if (!value) {
    return ''
  }
  var result = value.replace(/<br\s*\/?>/gi, '\r\n')
  return result
}
// 保留两位小数
exports.toFixed = function (value, length) {
  if (!value) {
    return '0.00'
  }
  if (typeof value == 'string') {
    value = parseFloat(value)
  }
  return value.toFixed(length)
}
// 物流状态名
exports.expressStatusTxt = function (value) {
  let txt = ''
  switch (value) {
    case '0':
      txt = '在途'
      break
    case '1':
      txt = '揽件'
      break
    case '2':
      txt = '疑难'
      break
    case '3':
      txt = '签收'
      break
    case '4':
      txt = '退签'
      break
    case '5':
      txt = '派件'
      break
    case '6':
      txt = '退回'
      break
    default:
      txt = ''
  }
  return txt
}

// 快递订单状态名
exports.orderStatusTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.orderStatus.cancle:
      txt = '订单已取消'
      break
    case dictionary.orderStatus.unpay:
      txt = '订单未付款'
      break
    case dictionary.orderStatus.payed:
      txt = '等待卖家发货'
      break
    case dictionary.orderStatus.sended:
      txt = '卖家已发货'
      break
    case dictionary.orderStatus.recevied:
      txt = '用户已收货'
      break
  }
  return txt
}

// 自提订单状态名
exports.orderStatusTxt2 = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.orderStatus.cancle:
      txt = '订单已取消'
      break
    case dictionary.orderStatus.unpay:
      txt = '订单未付款'
      break
    case dictionary.orderStatus.payed:
      txt = '等待卖家配货'
      break
    case dictionary.orderStatus.sended:
      txt = '卖家已配货'
      break
    case dictionary.orderStatus.recevied:
      txt = '用户已提货'
      break
  }
  return txt
}
// 租花状态名
exports.orderStatusTxt3 = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.orderStatus.cancle:
      txt = '订单已取消'
      break
    case dictionary.orderStatus.unpay:
      txt = '订单未付款'
      break
    case dictionary.orderStatus.payed:
    case dictionary.orderStatus.sended:
    case dictionary.orderStatus.recevied:
      txt = '订单付款成功'
      break
  }
  return txt
}
// 任务执行间隔时间单位
exports.taskIntervalTxt = function (value) {
  let txt = ''
  switch (value) {
    case '1':
      txt = '天'
      break
    case '2':
      txt = '周'
      break
    case '3':
      txt = '月'
      break
    case '4':
      txt = '年'
      break
  }
  return txt
}
// 任务执行间隔时间单位
exports.taskTypeTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.growthAction.watering:
      txt = '浇水'
      break
    case dictionary.growthAction.fertilize:
      txt = '施肥'
      break
    case dictionary.growthAction.basin:
      txt = '换盆'
      break
    case dictionary.growthAction.cut:
      txt = '修剪'
      break
    case dictionary.growthAction.grubs:
      txt = '病虫害'
      break
    case dictionary.growthAction.sunshine:
      txt = '日照'
      break
    case dictionary.growthAction.other:
      txt = '其他'
      break
  }
  return txt
}
// 任务执行间隔时间单位
exports.growthStatusTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.growthStatus.seed:
      txt = '种子期'
      break
    case dictionary.growthStatus.sprout:
      txt = '发芽期'
      break
    case dictionary.growthStatus.seedling:
      txt = '小苗期'
      break
    case dictionary.growthStatus.growthed:
      txt = '成株期'
      break
  }
  return txt
}
// 优惠券类型
exports.couponTypeTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.couponType.buy:
      txt = '买花'
      break
    case dictionary.couponType.rent:
      txt = '租花'
      break
  }
  return txt
}
// 优惠券使用条件文字
exports.couponUseTxt = function (value) {
  let txt = ''
  if (value) {
    txt = value == 0 ? '无门槛' : '满' + value + '可用'
  }
  return txt
}